﻿using System.Data;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;

namespace StudyCompanion.Core.Web;

public static class Json
{
    public static object ToJson(this string Json)
    {
        return Json == null ? null : JsonConvert.DeserializeObject(Json);
    }

    public static string ToJson(this object obj)
    {
        var timeConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
        return JsonConvert.SerializeObject(obj, timeConverter);
    }

    public static string ToJson(this object obj, string datetimeformats)
    {
        var timeConverter = new IsoDateTimeConverter { DateTimeFormat = datetimeformats };
        return JsonConvert.SerializeObject(obj, timeConverter);
    }

    public static T ToObject<T>(this string Json)
    {
        return Json == null ? default : JsonConvert.DeserializeObject<T>(Json);
    }

    public static List<T> ToList<T>(this string Json)
    {
        return Json == null ? null : JsonConvert.DeserializeObject<List<T>>(Json);
    }

    public static DataTable ToTable(this string Json)
    {
        return Json == null ? null : JsonConvert.DeserializeObject<DataTable>(Json);
    }

    public static JObject ToJObject(this string Json)
    {
        return Json == null ? JObject.Parse("{}") : JObject.Parse(Json.Replace("&nbsp;", ""));
    }

    /// <summary>
    ///     字符串转字典
    /// </summary>
    /// <param name="parameter">String</param>
    /// <returns>Dictionary</returns>
    public static Dictionary<string, string> ParseToDictionary(this string parameter)
    {
        try
        {
            var dataArry = parameter.Split('&');
            var dataDic = new Dictionary<string, string>();
            for (var i = 0; i <= dataArry.Length - 1; i++)
            {
                var dataParm = dataArry[i];
                var dIndex = dataParm.IndexOf("=");
                if (dIndex != -1)
                {
                    var key = dataParm.Substring(0, dIndex);
                    var value = dataParm.Substring(dIndex + 1, dataParm.Length - dIndex - 1);
                    dataDic.Add(key, value);
                }
            }

            return dataDic;
        }
        catch
        {
            return null;
        }
    }

    /// <summary>
    ///     字典转字符串
    /// </summary>
    /// <param name="parameters">Dictionary</param>
    /// <returns>String</returns>
    public static string ParseToString(this IDictionary<string, string> parameters)
    {
        IDictionary<string, string> sortedParams = new SortedDictionary<string, string>(parameters);
        var dem = sortedParams.GetEnumerator();

        var query = new StringBuilder("");
        while (dem.MoveNext())
        {
            var key = dem.Current.Key;
            var value = dem.Current.Value;
            if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
                query.Append(key).Append("=").Append(value).Append("&");
        }

        var content = query.ToString().Substring(0, query.Length - 1);

        return content;
    }
}